﻿using CrystalDecisions.CrystalReports.Engine;
using DSPL.Common.Classes;
using DSPL.Common.ViewModel;
using DSPL.Contracts.Common;
using DSPL.Contracts.Interfaces;
using DSPL.Reports.Classes;
using Microsoft.Practices.Unity;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;

namespace DSPL.Common
{
    /// <summary>
    /// Interaction logic for ReportToolBar.xaml
    /// </summary>
    public partial class ReportToolBar : UserControl
    {
        public ReportToolBar()
        {
            InitializeComponent();
            if (DesignerProperties.GetIsInDesignMode(this))
                return;

            ResolveDependancies();
        }

        #region Declaring Properties

        #region General Properties
       
        public IMessageService MessageService { get; set; }

        public string Title
        {
            get { return (string)GetValue(TitleProperty); }
            set { SetValue(TitleProperty, value); }
        }       

        public string ReportFolder
        {
            get { return (string)GetValue(ReportFolderProperty); }
            set { SetValue(ReportFolderProperty, value); }
        }

        public string ReportName
        {
            get { return (string)GetValue(ReportNameProperty); }
            set { SetValue(ReportNameProperty, value); }
        }
        
        public List<IReportParameters> AdditionalReportParameters
        {
            get { return (List<IReportParameters>)GetValue(AdditionalReportParametersProperty); }
            set { SetValue(AdditionalReportParametersProperty, value); }
        }
         
#endregion

        #region Visibility of Buttons

        public Visibility PrintVisibility
        {
            get { return (Visibility)GetValue(PrintVisibilityProperty); }
            set { SetValue(PrintVisibilityProperty, value); }
        }
        
        public Visibility CloseVisibility
        {
            get { return (Visibility)GetValue(CloseVisibilityProperty); }
            set { SetValue(CloseVisibilityProperty, value); }
        }
        
        #endregion
        
        #region Declaring Dependency Properties

        public static readonly DependencyProperty TitleProperty = DependencyProperty.Register("Title", typeof(string), typeof(ReportToolBar), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty ReportFolderProperty = DependencyProperty.Register("ReportFolder", typeof(string), typeof(ReportToolBar), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty ReportNameProperty = DependencyProperty.Register("ReportName", typeof(string), typeof(ReportToolBar), new PropertyMetadata(string.Empty));

        public static readonly DependencyProperty AdditionalReportParametersProperty = DependencyProperty.Register("AdditionalReportParameters", typeof(List<IReportParameters>), typeof(ReportToolBar), new PropertyMetadata(null));

        public static readonly DependencyProperty PrintVisibilityProperty = DependencyProperty.Register("PrintVisibility", typeof(Visibility), typeof(ReportToolBar), new PropertyMetadata(Visibility.Visible));

        public static readonly DependencyProperty CloseVisibilityProperty = DependencyProperty.Register("CloseVisibility", typeof(Visibility), typeof(ReportToolBar), new PropertyMetadata(Visibility.Visible));
        
        #endregion

        #endregion

        #region Exposable Event Handlers

        public event EventHandler<EventArgs> DataValidation;                    //use to data validate
        public event EventHandler<EventArgs> AddAddtionalReportParameters;      //use to adding addional parameters to report
        public event EventHandler<EventArgs> GenerateDataset;                   //use to generate dataset
       
        #endregion

        #region User Control Commands

        #region Print Button

        private void ExecutePrint(object sender, RoutedEventArgs e)
        {
            if (DataValidation != null)
                DataValidation(sender, e); 

            List<IReportParameters> repParaList = new List<IReportParameters>();
            repParaList.Add(new ReportParameters() { ParameterIndex = 0, ParameterName = "ReportName", InputValue = Title });
            repParaList.Add(new ReportParameters() { ParameterIndex = 1, ParameterName = "CompanyName", InputValue = GlobalVariables.CompanyName });
            repParaList.Add(new ReportParameters() { ParameterIndex = 2, ParameterName = "Address", InputValue = GlobalVariables.Address });
            repParaList.Add(new ReportParameters() { ParameterIndex = 3, ParameterName = "ContactDetails", InputValue = GlobalVariables.ContactDetails });
            repParaList.Add(new ReportParameters() { ParameterIndex = 4, ParameterName = "GeneratedUser", InputValue = GlobalVariables.LoggedInUser });
            repParaList.Add(new ReportParameters() { ParameterIndex = 5, ParameterName = "GenaratedMachine", InputValue = Environment.MachineName });
            repParaList.Add(new ReportParameters() { ParameterIndex = 6, ParameterName = "CopyRight", InputValue = GlobalVariables.CopyRight });
            repParaList.Add(new ReportParameters() { ParameterIndex = 7, ParameterName = "OnlineDetails", InputValue = GlobalVariables.OnlineDetails });

            if (AddAddtionalReportParameters != null)
            {
                AddAddtionalReportParameters(sender, e);          //use 8 as start ParameterIndex
                if (AdditionalReportParameters != null && AdditionalReportParameters.Count > 0)
                {
                    foreach (IReportParameters repParamItem in AdditionalReportParameters)
                        repParaList.Add(repParamItem);
                }

            }

            if(GenerateDataset!=null)
                GenerateDataset(sender,e);

            ReportsDocument ReportsDocument = new ReportsDocument();

            if (ReportsDocument != null)
            {
                ReportsDocument.InputValueList = repParaList;
                ReportDocument reportdoc;
                DataTable dtReport = new DataTable();
                reportdoc = ReportsDocument.GetReportDocument(ReportFolder + ":" + ReportName.Trim(),dtReport); ;
                if (reportdoc != null)
                {
                    ReportViewerViewModel vmReportWindow = new ReportViewerViewModel(reportdoc, Title);
                    if (vmReportWindow != null)
                        MessageService.ShowReport(vmReportWindow);
                }
            }
        }

        #endregion

        #region Close Button

        private void ExecuteClose(object sender, RoutedEventArgs e)
        {
            Window parentwin = Window.GetWindow(this);
            if (parentwin != null)
                parentwin.Close();
        }

        #endregion

        #endregion

        #region Internal events

        private void ResolveDependancies()
        {
            IUnityContainer container = GlobalVariables.GlobalUnityContainer;
            if (container != null)
            {
                MessageService = container.Resolve<IMessageService>();
            }
        }

        #endregion

    }
}
